package com.bcbim.authentication.role.service;

import com.bcbim.authentication.menu.mapper.AbilityMapper;
import com.bcbim.authentication.menu.mapper.MenuMapper;
import com.bcbim.authentication.role.mapper.RoleMapper;
import com.bcbim.authentication_interface.ability.dao.RoleAbilityDAO;
import com.bcbim.authentication_interface.role.dao.RoleMenuDAO;
import com.bcbim.authentication_interface.role.dto.RoleMenuAbilityDTO;
import com.bcbim.authentication_interface.role.service.RoleAuthenticationService;
import com.bcbim.authentication_interface.role.vo.MenuAbilityVO;
import com.bcbim.common.sys.Code;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description: 角色权限接口
 * @Author: ye
 * @Date: 2021/7/6
 * @Version: V1.0
 **/
@Service(timeout = 60 * 1000, group = "roleAuthentication", version = "1.0.0")
public class RoleAuthenticationImpl implements RoleAuthenticationService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private AbilityMapper abilityMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * @Description: 获取所有菜单
     * @Param: applicationPlatform
     * @Return: List<MenuAbilityVO>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<MenuAbilityVO> getAllMenuByApplication(String applicationPlatform) {
        return menuMapper.getAllMenuByApplication(applicationPlatform);
    }

    /**
     * @Description: 获取所有功能
     * @Param: applicationPlatform
     * @Return: List<MenuAbilityVO>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<MenuAbilityVO> getAllAbilityByApplication(String applicationPlatform) {
        return abilityMapper.getAllAbilityByApplication(applicationPlatform);
    }

    /**
     * @Description: 查询角色
     * @Param: roleName
     * @Return: List<Code>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<Code> selectRole(String roleName) {
        return roleMapper.selectRole(roleName);
    }

    /**
     * @Description: 获取角色拥有菜单权限
     * @Param: RoleMenuAbilityQuery
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> getRoleMenuAuthentication(RoleMenuAbilityDTO query) {
        return menuMapper.getRoleMenuAuthentication(query);
    }

    /**
     * @Description: 获取角色拥有功能权限
     * @Param: RoleMenuAbilityQuery
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> getRoleAbilityAuthentication(RoleMenuAbilityDTO query) {
        return abilityMapper.getRoleAbilityAuthentication(query);
    }

    /**
     * @Description: 获取对应的三级页面id
     * @Param: levelTwoId
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> getLevelThreeMenu(String levelTwoId) {
        return menuMapper.getLevelThreeMenu(levelTwoId);
    }

    /**
     * @Description: 根据页面id获取功能
     * @Param: List<String> 页面id
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> getAbilityByMenuId(List<String> childrenPage) {
        return abilityMapper.getAbilityByMenuId(childrenPage);
    }

    /**
     * @Description: 清空用户权限
     * @Param: menuId
     * @Param: arrayAbilityId
     * @Return:
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public void clearRoleAuthentication(String roleId, List<String> arrayMenuId, List<String> arrayAbilityId) {
        if (arrayMenuId.size() > 0) {
            menuMapper.clearRoleAuthentication(roleId, arrayMenuId);
        }
        if (arrayAbilityId.size() > 0) {
            abilityMapper.clearRoleAuthentication(roleId, arrayAbilityId);
        }
    }

    /**
     * @Description: 区分页面
     * @Param: String[] checkId
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> distinguishMenu(String[] checkId) {
        return menuMapper.distinguishMenu(checkId);
    }

    /**
     * @Description: 区分功能
     * @Param: String[] checkId
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public List<String> distinguishAbility(String[] checkId) {
        return abilityMapper.distinguishAbility(checkId);
    }

    /**
     * @Description: 保存角色权限
     * @Param: List<RoleMenuDO>
     * @Param: List<RoleAbilityDO>
     * @Return:
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public void saveRoleAuthentication(List<RoleMenuDAO> roleMenuList, List<RoleAbilityDAO> roleAbilityList) {
        if (roleMenuList.size() > 0) {
            menuMapper.saveRoleAuthentication(roleMenuList);
        }
        if (roleAbilityList.size() > 0) {
            abilityMapper.saveRoleAuthentication(roleAbilityList);
        }
    }

    /**
     * @Description: 查询根节点
     * @Param: levelTwoId
     * @Return: String
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public String getMenuRootId(String levelTwoId) {
        return menuMapper.getMenuRootId(levelTwoId);
    }

    /**
     * @Description: 获取二级菜单数量
     * @Param: roleId
     * @Param: rootId
     * @Return: int
     * @Author: ye
     * @Date: 2021/7/7
     */
    @Override
    public int countRoleLevelTwoMenu(String roleId, String rootId) {
        return menuMapper.countRoleLevelTwoMenu(roleId, rootId);
    }
}
